package la.iok.hzsvn.lewin.movie.shopmovie.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.shopmovie.param.CouponBaseFilterParam;
import la.iok.hzsvn.lewin.movie.core.shopmovie.param.CouponListParam;
import la.iok.hzsvn.lewin.movie.core.shopmovie.param.CouponParam;
import la.iok.hzsvn.lewin.movie.core.shopmovie.vo.CouponVo;
import la.iok.hzsvn.lewin.movie.shopmovie.model.CouponQRInfo;
import la.iok.hzsvn.lewin.movie.shopmovie.model.EfficacyCouponVo;
import la.iok.hzsvn.lewin.movie.shopmovie.service.CouponService;
import la.iok.hzsvn.lewin.mybatis.model.PageParam;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.lewin.mybatis.model.SortParam;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("coupons")
public class CouponController {
    private final CouponService couponService;

    public CouponController(CouponService couponService) {
        this.couponService = couponService;
    }

    @PostMapping("user/{userId}")
    public OperatorResponse save(@RequestHeader Token token, @PathVariable Long userId, @RequestBody CouponParam param){
        couponService.grant(token, userId, param);
        return OperatorResponses.success();
    }

    @PostMapping("qrcode")
    public SingleDataResponse<CouponQRInfo> qrCode(@RequestHeader Token token, @RequestBody CouponParam param){
        return new SingleDataResponse<>(couponService.create(token, param));
    }

    @DeleteMapping("{id}")
    public OperatorResponse delete(@RequestHeader Token token, @PathVariable Long id){
        couponService.delete(token, id);
        return OperatorResponses.success();
    }

    @GetMapping("{id}")
    public SingleDataResponse<CouponVo> find(@RequestHeader Token token, @PathVariable Long id){
        return new SingleDataResponse<>(couponService.select(token, id));
    }

    @GetMapping("uuid/{uuid}")
    public SingleDataResponse<CouponVo> find(@RequestHeader Token token, @PathVariable String uuid){
        return new SingleDataResponse<>(couponService.select(token, uuid));
    }

    /**
     * 用户领取优惠券
     * @param token 用户的令牌
     * @param uuid 优惠券标识
     * @return 操作结果
     */
    @GetMapping("qr/{uuid}")
    public OperatorResponse take(@RequestHeader Token token, @PathVariable String uuid){
        couponService.take(token, uuid);
        return OperatorResponses.success();
    }

    @GetMapping
    public SingleDataResponse<PageVo<CouponVo>> page(@RequestHeader Token token, PageParam pageParam, SortParam sortParam, CouponListParam listParam){
        return new SingleDataResponse<>(couponService.page(token, pageParam, sortParam, listParam));
    }

    @GetMapping("efficacy")
    public SingleDataResponse<PageVo<EfficacyCouponVo>> eifficacypage(@RequestHeader Token token, PageParam pageParam, SortParam sortParam, Long dayNum){
        return new SingleDataResponse<>(couponService.efficacypage(token, pageParam, sortParam, dayNum));
    }

    /**
     * 查询用户可使用优惠券
     * @param token 用户令牌
     * @param param 过滤参数
     * @return 可使用优惠券列表
     */
    @GetMapping("usable")
    public ListDataResponse<CouponVo> usable(@RequestHeader Token token, CouponBaseFilterParam param) {
        return new ListDataResponse<>(couponService.usable(token, param));
    }
}
